package com.rst.web;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.Currency;
import com.rst.model.InstituteDetail;
import com.rst.model.TimeZone;
import com.rst.service.IndexService;
import com.rst.validator.InstituteDetailsFormValidator;


@Controller
public class IndexController {
	
	@Autowired
	private IndexService indexService;
	
	@Autowired
	private InstituteDetailsFormValidator detailsFormValidator;

	@InitBinder
	public void initBinder(WebDataBinder binder) 
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}


	@RequestMapping(value="/newInstituteDetail", method=RequestMethod.GET)
	public ModelAndView newInstituteDetail(HttpServletRequest request )
	{
		ModelAndView mav = new ModelAndView("detail/createInstituteDetails");
		List<TimeZone> timeZones = indexService.getTimeZoneList();
		List<Currency> currencies = indexService.getCurrenciesList();
//		List<Country> country = indexService.getCountryList();
        HttpSession session = request.getSession();
        session.setAttribute("timeZones", timeZones);
        session.setAttribute("currencies",currencies);
  //      session.setAttribute("country",country);
		
        //List<StudentCategory> studentCategories = indexService.getStudentCategory();
		InstituteDetail detail = new InstituteDetail();
		mav.getModelMap().put("createInstituteDetails", detail);
		//mav.addObject("studentCategories", studentCategories);
		return mav;
	}
	
	@RequestMapping(value="/saveInstituteDetail", method=RequestMethod.POST)
	public String saveInstituteDetail(@ModelAttribute("instituteDetails")InstituteDetail detail, BindingResult result, SessionStatus status)
	{
		detailsFormValidator.validate(detail, result);
		if (result.hasErrors()) 
		{	
			return "detail/createInstituteDetails";
		}
		Long id = indexService.save(detail);
		status.setComplete();
		return "redirect:editInstituteDetail.htm?id="+id;
	}
	
	@RequestMapping(value="/editInstituteDetail", method=RequestMethod.GET)
	public ModelAndView editInstituteDetail(@RequestParam("id")Long instituteId , HttpServletRequest request)
	{
		ModelAndView mav = new ModelAndView("detail/editInstituteDetails");
		InstituteDetail instituteDetail = indexService.getInstituteDetailById(instituteId);
		mav.addObject("editInstituteDetail", instituteDetail);
		
		List<TimeZone> timeZones = indexService.getTimeZoneList();
		List<Currency> currencies = indexService.getCurrenciesList();
		//List<Country> country = indexService.getCountryList();
        HttpSession session = request.getSession();
        session.setAttribute("timeZones", timeZones);
        session.setAttribute("currencies",currencies);
        //session.setAttribute("country",country);

		return mav;
	}
	
	@RequestMapping(value="/updateInstituteDetail", method=RequestMethod.POST)
	public String updateInstituteDetail(@ModelAttribute("updateInstituteDetail") InstituteDetail detail, BindingResult result, SessionStatus status,
			@RequestParam("id")Long instituteId)
	{
		detailsFormValidator.validate(detail, result);
		if (result.hasErrors()) {
			return "detail/editInstituteDetails";
		}
		indexService.update(detail,instituteId);	
		status.setComplete();
		return "redirect:editInstituteDetail.htm?id="+instituteId;
	}
	@RequestMapping(value="/cityNameList", method=RequestMethod.GET)
	public void getCityList(HttpServletResponse response , HttpServletRequest request) throws Exception{
		response.setContentType("text/html");
		String name = request.getParameter("q");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print(indexService.getCityNameList(name));
		} finally {
			try {
				if(out != null) {
					out.flush();
					out.close();
				}
			} catch(Exception e) {}
		}
	}
	
	
	// for jsp desgine testing purpose 
	
	@RequestMapping(value="/test", method=RequestMethod.GET)
	public ModelAndView addClass()
	{
		ModelAndView mav = new ModelAndView("test");
		return mav;
	}

	@RequestMapping(value="/configuration", method=RequestMethod.GET)
	public ModelAndView configuration()
	{
		ModelAndView mav = new ModelAndView("login/configuration");
		return mav;
	}


}
